package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.Activities;
import com.hebeu.gfh.pojo.po.Clubs;
import com.hebeu.gfh.pojo.vo.activityAndClub;
import com.hebeu.gfh.pojo.vo.activityInfo;
import com.hebeu.gfh.pojo.vo.pages;
import com.hebeu.gfh.service.activitiesService;
import com.hebeu.gfh.service.clubsService;
import com.hebeu.gfh.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/activities")
public class activitiesController {

    @Autowired
    activitiesService activitiesService;
    @Autowired
    clubsService clubsService;

    @PostMapping("/findAllActivities")
    public R findAllActivities(pages page){
        List<Activities> activitiesList = activitiesService.findAllActivities(page);
        return R.ok(activitiesList);
    }
    @PostMapping("/findActivitiesByclubId")
    public R  findActivitiesByclubId(int clubId){
        List<Activities> activitiesList = activitiesService.findActivitiesByclubId(clubId);
        return R.ok(activitiesList);
    }

    @PostMapping("/findActivitiesByactivityId")
    public R findActivitiesByactivityId(int activityId,int userId){
         Activities activity = activitiesService.findActivitiesByactivityId(activityId);
        Clubs clubs = clubsService.findClubByclubId(activity.getClubId());
        activityAndClub activityAndClub = new activityAndClub(activity,clubs);
        int isapply = activitiesService.isparticipate(activity.getActivityId(),userId);
        //查询参加的人数
        int particiapant = activitiesService.findParticipant(activityId);
        activity.setParticipants(particiapant);
        Map result = new HashMap<>();
        result.put("activity",activity);
        result.put("clubs",clubs);
        result.put("isapply",isapply);
        return R.ok(result);
    }
    //根据userid查询所参加的活动
    @PostMapping("/findActivityByuserId")
    public R findActivityByuserId(int userId){
        List<Activities> activitiesList = activitiesService.findActivityByuserId(userId);
        return R.ok(activitiesList);
    }

    @PostMapping("/findAllActivity")
    public R findAllActivity(pages page){
        List<Activities> activitiesList = activitiesService.findAllActivities(page);
        return R.ok(activitiesList);
    }

    //获取活动总数
    @PostMapping("/findActivityNum")
    public R findActivityNum(){
        int num = activitiesService.findActivityNum();
        return R.ok(num);
    }
    //发布活动
    @PostMapping("/initActivity")
    public R initActivity(Activities activities){
        try {
            activitiesService.initActivity(activities);
        }catch (Exception e){
            System.out.println(e);
            return R.error(500,"发布失败");
        }
        return R.ok(activities.getActivityId());
    }

    //取消参加活动
    @PostMapping("/deleteActivityRegistration")
    public R  deleteActivityRegistration(int activityId,int userId){
        try {
            activitiesService.deleteActivityRegistration(activityId,userId);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("-------------------------------");
            return R.error(500,"取消参加失败");
        }
        return R.ok("取消参加成功");
    }

    //参加活动
    @PostMapping("/addActivityRegistration")
    public R addActivityRegistration(int acitivityId,int userId){
        try {
            activitiesService.addActivityRegistration(acitivityId,userId);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("-------------------------------");
            return R.error(500,"参加活动失败");
        }
        return R.ok("参加活动成功");
    }

        //获取活动信息
    @PostMapping("/getActivityInfo")
    public R  getActivityInfo(pages pages){
        List<Activities> activities =activitiesService.findActivityByuserId1(pages);
        List<activityInfo>  activityInfos = new ArrayList<>();
        for(Activities activity : activities){
            String clubName = clubsService.getClubNameById(activity.getClubId());
            activityInfo activityInfo = new activityInfo();
            activityInfo.setActivities(activity);
            activityInfo.setClubName(clubName);
            activityInfos.add(activityInfo);
        }
        int total = activitiesService.getActivityNum(pages.getUserId());
        Map result = new HashMap<>();
        result.put("activityInfos",activityInfos);
        result.put("total",total);
        return R.ok(result);
    }

    @PostMapping("/getActivityInfoByName")
    public R getActivityInfoByName(pages pages){
        List<Activities> activities =activitiesService.getActivityInfoByName(pages);
        List<activityInfo>  activityInfos = new ArrayList<>();
        for(Activities activity : activities){
            String clubName = clubsService.getClubNameById(activity.getClubId());
            activityInfo activityInfo = new activityInfo();
            activityInfo.setActivities(activity);
            activityInfo.setClubName(clubName);
            activityInfos.add(activityInfo);
        }
        int total = activitiesService.getActivityNum1(pages.getUserId());
        Map result = new HashMap<>();
        result.put("activityInfos",activityInfos);
        result.put("total",total);
        return R.ok(result);
    }

    //删除活动
    @PostMapping("/deleteActivityById")
    public R deleteActivityById(int activityId){
        try {
            activitiesService.deleteActivityById(activityId);
        }catch (Exception e){
            System.out.println("异常为"+e);
            return R.error(500,"删除失败");
        }
        return R.ok("删除成功");
    }
}
